<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="24">
          <el-form-item :label="$t('文档类型')" prop="dirName">
            <el-input v-model="form.dirName" :placeholder="$t('文档类型')" show-word-limit maxlength="127" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('备注')" prop="remark">
            <el-input v-model="form.remark" type="textarea" maxlength="2000" show-word-limit :placeholder="$t('备注')" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('归档目录')" prop="directoryId">
            <treeselect
              v-model="form.directoryId"
              :options="defaultDir"
              :flat="true"
              :multiple="false"
              :placeholder="$t('归档目录')"
              :normalizer="checkFileNormalizer"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存 取消按钮-->
      <save-btn has-permi="dd:template" />
    </div>
  </div>
</template>

<script>
import { saveDdTemplateDir, getDdTemplateDir } from '@/api/dd/template/ddTemplateDir'
import Treeselect from '@riophae/vue-treeselect'
import { leftListSysMenuConfigList } from '@/api/file/filefolder/folder.js'
export default {
  components: {
    Treeselect
  },
  data() {
    return {
      // 关联的文档库类型
      defaultDir: [],
      // 表单参数
      form: {},
      // 遮罩层
      loading: false,
      // 表单校验
      rules: {
        dirName: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ]
      }
    }
  },
  created() {
  },
  methods: {
    // 初始化数据
    init(data) {
      this.loading = true
      if (data != null && data.id !== undefined) {
        getDdTemplateDir(data.id).then(response => {
          this.form = response.data
          this.loading = false
        }).catch(() => {
          this.loading = false
        })
      } else {
        this.reset()
        this.form.parentId = data.parentId
        this.form.templateId = data.templateId
        this.loading = false
      }
      // 查询默认的文件夹
      this.getDefaultDir()
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        templateId: undefined,
        dirName: undefined,
        directoryId: undefined,
        remark: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        delFlag: undefined
      }
      this.resetForm('form')
    },
    // 取消按钮
    cancel() {
      this.$parent.$parent.formOpen = false
      this.$parent.$parent.reset()
    },
    // 关闭form页面
    closeForm(row) {
      console.log(row)
      // 关闭form页面遮罩层
      this.loading = false
      // 关闭form页面
      if (this.$parent.$parent.formOpen !== undefined) {
        this.$parent.$parent.formOpen = false
      }
      // 刷新list页面
      if (this.$parent.$parent.refreshDirList !== undefined) {
        this.$parent.$parent.refreshDirList()
      }
      // if (this.$parent.$parent.form.dirList) {
      //   this.$parent.$parent.form.dirList.push(row)
      // }
    },
    // 获取选填附件内容
    getDefaultDir() {
      leftListSysMenuConfigList({ 'sysRoleId': '1' }).then(res => {
        res.data.forEach(item => {
          this.checkFileNormalizer(item)
        })
        this.defaultDir = res.data
      })
    },
    // 格式化方法
    checkFileNormalizer(node) {
      if (!node.children || (node.children && node.children.length === 0)) {
        delete node.children
      }
      return {
        id: node.id,
        label: node.name,
        children: node.children
      }
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.loading = true
          saveDdTemplateDir(this.form).then(response => {
            if (response.code === 200) {
              this.msgSuccess(this.$t('保存成功'))
              this.closeForm(response.data)
            } else {
              this.msgError(response.msg)
            }
            this.loading = false
          })
        } else {
          this.locationError(document)
        }
      }).catch(() => {
        this.loading = false
      })
    }
  }
}
</script>
